window.onload = function() {
    document.querySelector('#btnCmd').setAttribute('data-cmd', 'start');
    document.querySelector('#btnCmd').innerHTML = '开始';
    document.querySelector('#btnCmd').onclick = btnCmd;

    ajax.config.state({
        201: true
    });
}

// 绑定按钮
function btnCmd(e) {
    var that = this,
        cmd = that.getAttribute('data-cmd');
    btnCmdFn[cmd]();
}

// 开始|结束
var btnCmdFn = {
    jude: false,
    // 反转 加(true)或者减(false)
    reversal: true,
    again: 1,
    start: function() {
        var self = btnCmdFn;
        self.jude = true;
        document.querySelector('#btnCmd').setAttribute('data-cmd', 'stop');
        document.querySelector('#btnCmd').innerHTML = '暂停';
        var timestart = document.querySelector('#timestart').value;
        var timestop = document.querySelector('#timestop').value;
        if(timestart <= timestop) {
            self.reversal = true;
        } else if(timestart >= timestop) {
            self.reversal = false;
        }
        initVolist(timestart, timestop);
    },
    stop: function() {
        var self = btnCmdFn;
        self.jude = false;
        document.querySelector('#btnCmd').setAttribute('data-cmd', 'start');
        document.querySelector('#btnCmd').innerHTML = '开始';
        document.querySelector('#timestart').value = self.timestart;
    },
    timestart: 0
}

// 查询
function initVolist(timestart, timestop) {
    var data_opt = {
        no: timestart
    }
    var stime = new Date().getTime();
    ajax.post('/api?pk10.exactCollect', data_opt, function(res) {
        res['no'] = data_opt.no;
        res['stime'] = stime;
        // 显示结果
        createDatabase(res);
        if(res.status == 0) {
            // 重复执行
        } else if (res.status == 400) {
            // 停止采集,控制台输出提示消息
            btnCmdFn.jude = false;
        } else {
            if(res.status == 500 && btnCmdFn.again < 3) {
                // 重复执行3次
                ++btnCmdFn.again;
            } else {
                btnCmdFn.again = 1;
                // 继续执行
                if(btnCmdFn.reversal == true) {
                    ++timestart;
                } else if(btnCmdFn.reversal == false) {
                    --timestart;
                }
            }
            btnCmdFn.timestart = timestart;
        }
        // 是否暂停
        if(btnCmdFn.jude == true) {
            // 继续执行
            if((btnCmdFn.reversal == true && timestart <= timestop) || (btnCmdFn.reversal == false && timestart >= timestop)) {
                initVolist(timestart, timestop);
            } else {
                enforcement();
            }
        }
    });
}

// 创建
function createDatabase(res) {
    var list = res;
    var etime = new Date().getTime();
    var ele = '';
    ele += '<font class="p-r-2x">' + list.no + '期</font>';
    ele += '<font class="p-r-2x">' + list.status + '</font>';
    if(list.status == 201) {
        ele += '<font class="p-r-2x">' + list.data + '</font>';
    } else {
        if(list.status == 0) {
            list.data = '请求无响应';
        } else if(list.status == 500) {
            list.data = '请求异常';
        }
        ele += '<font class="p-r-2x c-err">' + list.data + '</font>';
    }
    //ele += '<font class="p-r-2x">请求时间：' + tool.formatDate('hh:ii:ss', res.stime) + '</font>';
    //ele += '<font class="p-r-2x">响应时间：' + tool.formatDate('hh:ii:ss', etime) + '</font>';
    ele += '<font class="">耗时：' + (etime - res.stime) + 'ms</font>';
    var domli = document.createElement('li');
    domli.innerHTML = ele;
    document.querySelector('#databaseVlist').insertBefore(domli, document.querySelector('#databaseVlist').getElementsByTagName('li')[0]);
}

// 请求结束
function enforcement() {
    var ele = '';
    ele += '<font class="p-r-2x c-err">请求结束</font>';
    var domli = document.createElement('li');
    domli.innerHTML = ele;
    document.querySelector('#databaseVlist').insertBefore(domli, document.querySelector('#databaseVlist').getElementsByTagName('li')[0]);
}